import axios from 'axios'

// 创建axios实例
const api = axios.create({
  baseURL: 'http://127.0.0.1:9999/symdb',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    console.error('API请求错误:', error)
    return Promise.reject(error)
  }
)

// API方法
export const getProducts = async () => {
  return await api.get('/products')
}

export const getModules = async (product, version) => {
  return await api.get(`/products/${product}/${version}/modules`)
}

export const getModuleDetails = async (product, version, moduleId) => {
  return await api.get(`/products/${product}/${version}/modules/${moduleId}/details`)
}

export const getModuleFields = async (product, version, moduleId, type = 'deps') => {
  return await api.get(`/products/${product}/${version}/modules/${moduleId}/fields`, {
    params: { type }
  })
}

export const getModuleDependencies = async (product, version, moduleId) => {
  return await api.get(`/products/${product}/${version}/deps/${moduleId}/symbols`)
}

export const getGraphData = async (product, version, graphType, params = {}) => {
  return await api.get(`/graphviz/${product}/${version}/${graphType}`, { params })
}

export const getAnalysisData = async (product, version, params = {}) => {
  return await api.get(`/analysis/${product}/${version}`, { params })
}

export const getDashboardData = async (product, version) => {
  return await api.get(`/dashboard/${product}/${version}`)
}

export const getTestCases = async (product, version) => {
  return await api.get(`/products/${product}/${version}/records`)
}

export const getTestCaseProcesses = async (product, version, recordId) => {
  return await api.get(`/products/${product}/${version}/records/${recordId}/processes`)
}

export const getTestCaseRegionTypes = async (product, version, recordId) => {
  return await api.get(`/products/${product}/${version}/records/${recordId}/regiontypes`)
}

export const getProcessesByName = async (product, version, recordId, processId) => {
  return await api.get(`/products/${product}/${version}/records/${recordId}/processes_by_name`, {
    params: { id: processId }
  })
}

export default api